/**
*  Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
*/
package cn.knet.sp.ncrm.common.websocket;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

/**
 * TODO(这里用一句话描述这个类的作用)
 * @author Gehaiyang
 * @date 2015年7月13日 下午3:04:58
 * 
 */
public class WebSocketHandshakeInterceptor implements HandshakeInterceptor {
	 
    private static Logger logger = LoggerFactory.getLogger(WebSocketHandshakeInterceptor.class);
    
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, 
    		WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            HttpSession session = servletRequest.getServletRequest().getSession(false);
            if (session != null) {
                //获取登录用户的信息
                attributes.put("user",UserUtils.getUser());
                logger.debug("Get login user info success......");
            }
        }
        return true;
    }
 
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
 
    }
}
